package com.lepu.app.fun.mother_circle.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.app.utils.AppEnterTongJi;
import com.app.utils.DateUtil;
import com.app.utils.MyNetUtil;
import com.core.lib.application.BaseFragmentActivity;
import com.eyzhs.app.R;
import com.lepu.app.fun.index.activity.IndexWriterBannerActivity;
import com.lepu.app.fun.lottery.AppLotteryActivity;
import com.lepu.app.fun.mother_circle.bean.BeanMotherMessage;
import com.lepu.app.fun.my.PubContentDetailActivity;
import com.lepu.app.fun.my.bean.BeanMessage;
import com.lepu.app.main.activity.AppBroswerActivity;

import java.util.ArrayList;

@SuppressWarnings("unused")
public class MessageListAdapter extends BaseAdapter {

    private LayoutInflater mLayoutInflater = null;
    private Context mContext = null;
    private ArrayList<BeanMessage> mArrayList = null;

    public MessageListAdapter(Context context,ArrayList<BeanMessage> arrayList) {
        mContext = context;
        mLayoutInflater = LayoutInflater.from(context);
        mArrayList = arrayList;
    }

    @Override
    public int getCount() {
        return mArrayList.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("InflateParams")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = mLayoutInflater.inflate(R.layout.my_item_message, null);
            holder = new ViewHolder();
            holder.txt_content = (TextView) convertView.findViewById(R.id.txt_content);
            holder.txt_time = (TextView) convertView.findViewById(R.id.txt_time);
            holder.ic_new = (ImageView) convertView.findViewById(R.id.ic_new);
            holder.itemLayout = (LinearLayout) convertView.findViewById(R.id.itemLayout);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final BeanMessage item = mArrayList.get(position);
        holder.txt_content.setText(item.MessageContent);
        holder.txt_time.setText(DateUtil.getHMTime(item.MessageTime));
        holder.ic_new.setVisibility(item.IsRead == 1 ? View.INVISIBLE : View.VISIBLE);
        
        holder.itemLayout.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {

				if (item.LinkTargetType.equals("-1")) {
					
					MyNetUtil.getInstance().requestArticleDetail(((BaseFragmentActivity)mContext), item.LinkTargetID,"", "","");
				} else if (item.LinkTargetType.equals("0")) {

				} else if (item.LinkTargetType.equals("1")) {
					// 优酷视频播放
					// video
					// 先跳对应的详情页面
					// Utils.jumpVideoInfo(MainTabActivity.getInstance(),
					// item.LinkTargetID);
					
					MyNetUtil.getInstance().requestArticleDetail(
							((BaseFragmentActivity)mContext), item.LinkTargetID,
							"", "","");

				} else if (item.LinkTargetType.equals("2")) {
					// 月子大咖详情（晒经验详情）
					// 已经没有了这个功能
				} else if (item.LinkTargetType.equals("3")) {
					
					AppEnterTongJi.clickEvent(74, item.LinkTargetID);
					
					// 宝马一起秀详情
					Intent intent = new Intent(mContext,
							PubContentDetailActivity.class);
					intent.putExtra("tag", 0);
					intent.putExtra("TopicId", item.LinkTargetID);
					((BaseFragmentActivity)mContext).startActivity(intent, true);
				} else if (item.LinkTargetType.equals("4")) {
					
					AppEnterTongJi.clickEvent(74, item.LinkTargetID);
					
					// 情感八卦详情
					Intent intent = new Intent(mContext,PubContentDetailActivity.class);
					intent.putExtra("tag", 1);
					intent.putExtra("TopicId", item.LinkTargetID);
					((BaseFragmentActivity)mContext).startActivity(intent, true);
				} else if (item.LinkTargetType.equals("5")) {
					// Intent intent = new Intent(mContext,
					// MyArticleListActivity.class);
					// intent.putExtra("title", "宝宝发育指南");
					// ((BaseFragmentActivity) mContext).startActivity(intent,
					// true);
					// 现在网页做了
					MyNetUtil.getInstance().requestArticleDetail(
							((BaseFragmentActivity)mContext), item.LinkTargetID,"", "","");
				} else if (item.LinkTargetType.equals("6")) {
					
					AppEnterTongJi.clickEvent(74, item.LinkTargetID);
					
					// 抽奖
					Intent lotteryIntent = new Intent(mContext,AppLotteryActivity.class);
					lotteryIntent.putExtra("LinkTargetID", item.LinkTargetID);// 抽奖活动id
					((BaseFragmentActivity)mContext).startActivity(lotteryIntent, true);
				} else if (item.LinkTargetType.equals("100")) {
					
					AppEnterTongJi.clickEvent(74, item.LinkURL);
					
					// 跳转指定链接
					Intent intent = new Intent(mContext,AppBroswerActivity.class);
					intent.putExtra("need_share", true);
					intent.putExtra("title", "");
					intent.putExtra("cover", "");
					intent.putExtra("url", item.LinkURL);
					((BaseFragmentActivity)mContext).startActivity(intent, true);
				}else if(item.LinkTargetType.equals("8")){
					AppEnterTongJi.clickEvent(74, item.LinkURL);
					
					MyNetUtil.getInstance().requestArticleDetailWriter((BaseFragmentActivity)mContext, item.LinkTargetID);
				}
			
			}
		});
        return convertView;
    }

    private static class ViewHolder {
    	LinearLayout itemLayout;
        TextView txt_content;
        TextView txt_time;
        ImageView ic_new;
    }

}
